import { _decorator, Component, EventTouch, Node } from 'cc';
import { PatternConfig, patternConfigs } from './Type/Type';
const { ccclass, property } = _decorator;

@ccclass('Scene')
export class Scene extends Component {
    @property(Node)
    clickArea: Node = null!;

    start() {
        this.clickArea.on(Node.EventType.TOUCH_END, this.onClick, this);
    }

    onClick(event: EventTouch) {
        const pos = event.getLocation();
        console.log('点击位置:', pos);
        //点击随机一个图案
        const patternType = this.randomPatternType();
    }

    randomPatternType() {
        const keys = Object.keys(patternConfigs);
        const randomIndex = Math.floor(Math.random() * keys.length);
        return keys[randomIndex] as unknown as PatternConfig;
    }
    //生成方块形状
    generatePattern(patternConfig: PatternConfig, enableColorVariation: boolean = true, variationIntensity: string = 'medium') {
        const { pattern, pixelSize, pixelColor, backgroundColor } = patternConfig;
        const patternHeight = pattern.length;
        const patternWidth = pattern[0].length;
        const width = patternWidth * pixelSize;
        const height = patternHeight * pixelSize;
        const pixelData = new Uint8Array(width * height * 4);
        const logicPixelColors: [number, number, number, number][][] = [];
        for (let logicY = 0; logicY < patternHeight; logicY++) {
            logicPixelColors[logicY] = [];
            for (let logicX = 0; logicX < patternWidth; logicX++) {
                if (pattern[logicY][logicX] === 1) {
                    // 有像素 - 生成该逻辑像素的随机颜色
                    if (enableColorVariation) {
                        logicPixelColors[logicY][logicX] = this.getPixelVariation(logicX, logicY, pixelColor, variationIntensity);
                    } else {
                        logicPixelColors[logicY][logicX] = pixelColor; // 无变化的原色
                    }
                } else {
                    logicPixelColors[logicY][logicX] = backgroundColor; // 背景色
                }
            }
        }
         // 按逻辑像素块填充，避免坐标映射误差
        for (let ly = 0; ly < patternHeight; ly++) {
            for (let lx = 0; lx < patternWidth; lx++) {
                const color = logicPixelColors[ly][lx];
                // console.log(color,ly,lx,'color')
                const startX = lx * pixelSize;
                const startY = ly * pixelSize;
                const endX = startX + pixelSize;
                const endY = startY + pixelSize;

                for (let y = startY; y < endY; y++) {
                    for (let x = startX; x < endX; x++) {
                        const index = (y * width + x) * 4;
                        pixelData[index] = color[0];
                        pixelData[index + 1] = color[1];
                        pixelData[index + 2] = color[2];
                        pixelData[index + 3] = color[3];
                        // console.log('color',color,index)
                    }
                }
            }
        }
    }
    getPixelVariation(logicX: number, logicY: number, baseColor: [number, number, number, number], intensity: string = 'medium'): [number, number, number, number] {
        const factorsSubtle = [0.92, 0.97, 1.0, 1.03, 1.08];
        const factorsMedium = [0.85, 0.93, 1.0, 1.07, 1.15];
        const factorsStrong = [0.7, 0.85, 1.0, 1.15, 1.3];
        const table = intensity === 'strong' ? factorsStrong : intensity === 'subtle' ? factorsSubtle : factorsMedium;
        const idx = (logicX + logicY) % table.length;
        const k = table[idx];
        const r = Math.max(0, Math.min(255, Math.round(baseColor[0] * k)));
        const g = Math.max(0, Math.min(255, Math.round(baseColor[1] * k)));
        const b = Math.max(0, Math.min(255, Math.round(baseColor[2] * k)));
        return [r, g, b, baseColor[3]];
    }
    update(deltaTime: number) {
        
    }
}

